package com.example.jvm.reference;

import java.lang.ref.WeakReference;
import java.util.HashMap;

public class TestJava2 {

    public static void main(String[] args) {

        HashMap map = new HashMap();
        WeakReference t1 = new WeakReference<>(new Test2());
        WeakReference t2 = new WeakReference<>(new Test2());
        map.put(t1, "1");
        map.put(t2, "2");
        t1 = null;
        System.gc();
        System.out.println("第1步" + map);
        System.out.println("第1步 get t2: " + map.get(t2));

        t2 = null;
        System.gc();
        System.out.println("第2步" + map);

        map.clear();
        System.gc();
        System.out.println("第3步" + map);

    }


}

class Test2 {
    private String strTest = "该Test对象还存在";

    @Override
    public String toString() {
        return strTest;
    }

    @Override
    protected void finalize() throws Throwable {
        // TODO Auto-generated method stub
        System.out.println("该Test对象被释放了");
    }

}
